/*
1.子线程传入结构体
2.主线程通过argc，argv传入参数
3.主线程调用pthread_exit函数退出，保证子线程继续运行
*/

#include "spu.h"

struct student
{
    int age;
    char name[20];
};

void *thread_fun(void *stu)
{
    /* 这里如果加sleep，main执行完return后，子线程将得不到执行 */
    // sleep(1);  

	printf("age = %d, name = %s\n", ((struct student*)stu)->age, ((struct student*)stu)->name);

	return (void*)0;
}

int main(int argc, char *argv[])
{	
	int err;
	pthread_t thread_id; 
    struct student stu;
    void *__retval;

    stu.age = 20;
    memcpy(stu.name, "xiaoming", 20);

	err = pthread_create(&thread_id, NULL, thread_fun, &stu);
	if(err != 0)
		printf("create fail, err = %d\n",err);
	else
		printf("create success!\n");

	for(int i = 0; i < argc; i++)
    {
        printf("args[%d] = %s\n", i, argv[i]);
    }

    /* 使用该函数退出main线程后，子线程会继续执行 */
	pthread_exit(__retval);
}

